﻿Imports DTO
Imports BUS
Imports System.Windows.Forms
Public Class frmLoaiPin
    Private Sub LoadData()
        Dim ds As List(Of LoaiPinDTO) = BUS.LoaiPinBUS.ReadData()
        dgvDuLieuThem.DataSource = ds
    End Sub
    Private _trangThaiDangNhap As Boolean
    Public Sub TrangThai(trangthai As Boolean)
        _trangThaiDangNhap = trangthai
    End Sub
    Private Sub frmLoaiPin_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim mdiparent As Main = DirectCast(Me.MdiParent, Main)
        If mdiparent.DaDangNhap = False Then
            If MessageBox.Show("Bạn chưa nhập Mật Khẩu để thực hiện chỉnh sửa Cơ Sở Dữ Liệu. Bạn có muốn nhập Mật Khẩu không?", "Thông Báo", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) = Windows.Forms.DialogResult.Yes Then
                Dim frmMatKhau As New NhapMatKhau()
                'frmMatKhau.MdiParent = Me.MdiParent
                Dim result = frmMatKhau.ShowDialog()
                If result = Windows.Forms.DialogResult.Cancel Then
                    Me.Close()
                ElseIf result = Windows.Forms.DialogResult.OK Then

                    mdiparent.DangNhap()
                End If
            Else
                Me.Close()
            End If
        End If
        LoadData()
    End Sub

    Private Sub btnThem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThem.Click
        Dim loaipin As New LoaiPinDTO()
        loaipin.TenLoai = txtTenLoai.Text
        loaipin.DonGia = txtDonGia.Text

        Dim insert As New LoaiPinBUS()
        insert.InsertData(loaipin.TenLoai, loaipin.DonGia)
        LoadData()
    End Sub

    Private Sub btnSua_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSua.Click
        Dim loaipin As New LoaiPinDTO()
        loaipin.MaLoai = BUS.LoaiPinBUS.ReadData()(dgvDuLieuThem.CurrentRow.Index).MaLoai
        loaipin.TenLoai = txtTenLoai.Text
        loaipin.DonGia = Convert.ToInt32(txtDonGia.Text)

        Dim update As New LoaiPinBUS()
        update.UpdateData(loaipin.MaLoai, loaipin.TenLoai, loaipin.DonGia)
        LoadData()
    End Sub

    Private Sub btnXoa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnXoa.Click
        Dim loaipin As New LoaiPinDTO()
        loaipin.MaLoai = BUS.LoaiPinBUS.ReadData()(dgvDuLieuThem.CurrentRow.Index).MaLoai

        Dim delete As New LoaiPinBUS()
        delete.DeleteData(loaipin.MaLoai)
        LoadData()
    End Sub

    Private Sub dgvDuLieuThem_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvDuLieuThem.CellClick
        Dim i As Integer
        i = dgvDuLieuThem.CurrentRow.Index
        txtTenLoai.Text = dgvDuLieuThem.Item(1, i).Value
        txtDonGia.Text = dgvDuLieuThem.Item(2, i).Value
    End Sub

    Private Sub btnThoat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThoat.Click
        Me.Close()
    End Sub
End Class